<?php
/*
微信企业 支付到个人
*/
class cash
{

    /**
     *  array转xml
     */
    public function arrayToXml($arr)
    {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            } else
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
        }
        $xml .= "</xml>";
        return $xml;
    }

    //使用证书，以post方式提交xml到对应的接口url
    /**
     *   作用：使用证书，以post方式提交xml到对应的接口url
     */
    function curl_post_ssl($url, $xml, $second=30)
    {
        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);

        //以下两种方式需选择一种
        /******* 此处必须为文件服务器根目录绝对路径 不可使用变量代替*********/
        curl_setopt($ch,CURLOPT_SSLCERT,"./apiclient_cert.pem");
        curl_setopt($ch,CURLOPT_SSLKEY,"./apiclient_key.pem");


        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);

        $data = curl_exec($ch);
        if($data){
            curl_close($ch);
            return $data;
        }
        else {
            $error = curl_errno($ch);
            echo "call faild, errorCode:$error\n";
            curl_close($ch);
            return false;
        }
    }

    //企业向个人付款
    public function payToUser($openid,$desc,$amount)
    {
        //微信付款到个人的接口
        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';

        $params["mch_appid"]        = 'wx13b0ce87f8202ad4';   //公众账号appid
        $params["mchid"]            = '1246708301';   //商户号 微信支付平台账号
        $params["nonce_str"]        = 'longdongzhiye99'.mt_rand(100,999);   //随机字符串
        $params["partner_trade_no"] = mt_rand(10000000,99999999);           //商户订单号
        $params["amount"]           = $amount;          //金额
        $params["desc"]             = $desc;            //企业付款描述
        $params["openid"]           = $openid;          //用户openid
        $params["check_name"]       = 'NO_CHECK';       //不检验用户姓名
        $params['spbill_create_ip'] = $_SERVER['HTTP_HOST'];   //获取IP

        //生成签名
        $str = 'amount='.$params["amount"].'&check_name='.$params["check_name"].'&desc='.$params["desc"].'&mch_appid='.$params["mch_appid"].'&mchid='.$params["mchid"].'&nonce_str='.$params["nonce_str"].'&openid='.$params["openid"].'&partner_trade_no='.$params["partner_trade_no"].'&spbill_create_ip='.$params['spbill_create_ip'].'&key=F5YguNW77Ao4N5yu5wZ8Lb00NKOg1Y04';
        //md5加密 转换成大写
        $sign = strtoupper(md5($str));
        //签名
        $params["sign"] = $sign;
        //array转xml
        $xml = $this->arrayToXml($params);
        //证书请求支付
        return $this->curl_post_ssl($url, $xml);
    }

}

/*
    $cash = new cash;
    $res = $cash -> payToUser();
    var_dump($res);
*/